package com.five.controller.reception;

import com.alibaba.fastjson.JSON;
import com.five.dao.reception.UserDao;
import com.five.dao.impl.reception.UserDaoImpl;
import com.five.entity.backstage.TUser;
import com.five.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/findUser")
public class UserControl extends HttpServlet {
    UserDao userDao=new UserDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method=req.getParameter("method");
        if(method!=null){
            switch (method){
                case "del":
                    delete(req,resp);
                    break;
                case "register":
                    register(req,resp);
                case "add":
                    add(req, resp);
                    break;
                case "upd":
                    upd(req, resp);
                    break;
                case "findByIdCard":
                    findByIdCard(req, resp);
                    break;
                case "findByPhone":
                    findByPhone(req, resp);
                    break;
                case "findByNamePwd":
                    findByNamePwd(req, resp);
                    break;
                case "login":
                    login(req,resp);
                    break;
                case "loginOut":
                    loginOut(req,resp);
                    break;
                case "update":
                    update(req,resp);
                default:break;
            }
        }else{
            //查询出所有的客户信息
            List<Map> maps = userDao.queryAll();
            Map m = new HashMap();
            m.put("code",0);
            m.put("msg","");
            m.put("count",maps.size());
            m.put("data",maps);

            String map = JSON.toJSONString(m);
            //把这个数据传递到页面 out
            PrintWriter out = resp.getWriter();
            out.print(map);
            out.flush();
            out.close();

            /*req.setAttribute("ulist",maps);
            //携带页面 请求转发 重定向
           req.getRequestDispatcher("/queryAll").forward(req,resp);
*/
        }
    }

    //越：修改信息
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pwd = req.getParameter("pwd");
        String id = req.getParameter("id");
        String newpwd = req.getParameter("newpwd");
        PrintWriter out = resp.getWriter();
        out.print(userDao.updatae(id,pwd,newpwd));
        out.flush();
        out.close();
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        List<Map> maps = this.userDao.findName(name,password);
        Object name1 = maps.get(0).get("name");
        String s = name1.toString();
        Map map = maps.get(0);
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        if (maps.size()>0){
            writer.print("1");
            session.setAttribute("name1",map);
        }else {
            writer.print("0");
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除当前id的值 删除 数据
        String uid=req.getParameter("uid");
        // dao 删除
        int i = userDao.delUser(uid);
        if(i>0){
            resp.sendRedirect("user/show.jsp");
        }
    }
    /*
    * 注册新客户
    * */
    private void register(HttpServletRequest req, HttpServletResponse resp) {
        //获取表单里的数据
        Map<String, String[]> parameterMap = req.getParameterMap();
        TUser u = new TUser();
        TransBean.populate(u,parameterMap);
        int i = userDao.register(u);
    }
/*
* 添加客户
* */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单里的数据
        Map<String, String[]> parameterMap = req.getParameterMap();
        TUser u = new TUser();
        TransBean.populate(u,parameterMap);
        int i = userDao.add(u);
        PrintWriter out = resp.getWriter();
        out.print(i);
        out.flush();
        out.close();
    }
/*
* 修改客户信息
* */
    protected void upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        TUser u = new TUser();
        TransBean.populate(u,parameterMap);
        //根据客户的id 查询用户的信息
        int m = userDao.add(u);
        resp.sendRedirect("findUser");
    }
/*
* 通过IDcard查找
* */
    protected void findByIdCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String idCard = req.getParameter("idcard");
        // dao 删除
        List<Map> map = userDao.findUserByIdCard(idCard);
        if (map.size() > 0) {
            out.print(0);
        } else {
            out.print(1);
        }
        out.flush();
        out.close();
    }
    /*
     * 通过IDcard查找
     * */
    protected void findByPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String phone = req.getParameter("phone");
        // dao 删除
        List<Map> map = userDao.findUserByPhone(phone);
        if (map.size() > 0) {
            out.print(0);
        } else {
            out.print(1);
        }
        out.flush();
        out.close();
    }
    protected void findByNamePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        // dao 传到后台执行查询
        List<Map> map = userDao.findUserByNamePwd(name,pwd);
        if (map.size() > 0) {
            out.print(0);
        } else {
            out.print(1);
        }
        //out.flush();
       // out.close();
    }

    private void loginOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("name1");
        //登录页面
        resp.sendRedirect("/Hotel/view/reception/reservelogin.jsp");
    }
}
